package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.TaskVersionConfig;
import com.novax.ex.activity.open.api.inner.TaskVersionConfigApi;
import com.novax.ex.activity.open.model.query.TaskVersionConfigQuery;
import com.novax.ex.activity.open.model.request.task.TaskVersionConfigRequest;
import com.novax.ex.activity.open.model.response.task.TaskVersionConfigResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.TaskVersionConfigService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.enums.TaskVersionConfigStatus;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.Objects;

/**
 * @Description 版本配置 controller
 * @Author ChenXi
 * @Date 2023/06/21/10:34
 */

@RestController
public class TaskVersionConfigController extends BaseActivityController implements TaskVersionConfigApi {

    @Resource
    private TaskVersionConfigService taskVersionConfigService;

    @Override
    public ReturnResult<PageUtil<TaskVersionConfigResponse>> page(TaskVersionConfigQuery query) {
        PageUtil<TaskVersionConfigResponse> pageUtil = new PageUtil<>(query);
        pageUtil.setCondition(Collections.singletonMap("query", query));
        taskVersionConfigService.findPage(pageUtil, TaskVersionConfigResponse.class);
        return ReturnResult.success(pageUtil);
    }

    @Override
    public ReturnResult<?> modify(String language, Long id, TaskVersionConfigRequest taskVersionConfigRequest) {
        TaskVersionConfig taskVersionConfig = taskVersionConfigService.find(id);
        if(Objects.isNull(taskVersionConfig)){
            return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
        }

        if(Objects.isNull(taskVersionConfigRequest)){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        Integer configState = taskVersionConfig.getState();
        Integer requestState = taskVersionConfigRequest.getState();
        // 上线之后的版本不可下线
        if(configState.equals(TaskVersionConfigStatus.online.getStatus())
           && Objects.nonNull(requestState)
           && requestState.equals(TaskVersionConfigStatus.offline.getStatus())){
            return ReturnResult.fail(getInternational(language,InternationalCode.ONLINE_VERSION_CANNOT_OFF_IN));
        }

        TaskVersionConfig requestTaskVersionConfig = CopyUtils.copyObject(taskVersionConfigRequest, TaskVersionConfig.class);
        Date now = new Date();
        // 如果此次修改上线的版本 更新上线时间
        if(configState.equals(TaskVersionConfigStatus.offline.getStatus())
           && Objects.nonNull(requestState)
           && requestState.equals(TaskVersionConfigStatus.online.getStatus())){
            requestTaskVersionConfig.setOnlineTime(now);
        }

        requestTaskVersionConfig.setId(id);
        boolean flag = taskVersionConfigService.modifySelective(requestTaskVersionConfig);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> delete(String language, Long id) {
        TaskVersionConfig taskVersionConfig = taskVersionConfigService.find(id);
        if(Objects.isNull(taskVersionConfig)){
            return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
        }

        // 上线之后不可删除
        if (taskVersionConfig.getState().equals(TaskVersionConfigStatus.online.getStatus())){
            return ReturnResult.fail(getInternational(language, InternationalCode.ONLINE_VERSION_CANNOT_DELETE));
        }

        // 删除所有的新手任务配置
        boolean flag = taskVersionConfigService.removeTask(id);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> add(String language, TaskVersionConfigRequest taskVersionConfigRequest) {
        if(Objects.isNull(taskVersionConfigRequest) || !StringUtils.isNotBlank(taskVersionConfigRequest.getInnerName())){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        // 新建版本需为未上线状态
        taskVersionConfigRequest.setState(TaskVersionConfigStatus.offline.getStatus());
        boolean flag =  taskVersionConfigService.addTaskVersionConfig(taskVersionConfigRequest);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));

    }

}
